package com.songshuai.webapi.permission.controller;

import com.songshuai.webapi.baseapi.utils.OssUploadUtil;
import com.songshuai.webapi.baseapi.utils.RegexUtil;
import com.songshuai.webapi.baseapi.utils.SnowFlakeUtil;
import com.songshuai.webapi.common.redis.RedisUtil;
import com.songshuai.webapi.permission.config.redislock.RedisLockImpl;
import com.songshuai.webapi.permission.dto.po.UserHasPermissionPojo;
import com.songshuai.webapi.permission.model.YxUsers;
import com.songshuai.webapi.permission.service.UsersService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by yubo on 2018/11/21  .
 */

public class BaseController {


    Logger logger = LoggerFactory.getLogger(BaseController.class);

    @Autowired
    private UsersService usersService;

    @Autowired
    protected   RedisUtil redisUtil;

    @Autowired
    private StringRedisTemplate redisTemplate;






    /*
    获取当前登录用户的用户名
     */
    protected String getUserName() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        String name= String.valueOf(principal);

        return  name;
    }

    /*
    获取当前登录用户的用户ID
     */
    protected Integer getUserId() {
        return  getUserBaseInfo().getId();
    }

    /*
    获取当前用户基本信息
     */
    protected YxUsers getUserBaseInfo(){
        String username=this.getUserName();
        int integer = RegexUtil.validPhoneOrMail(username);
        YxUsers info = usersService.getUserInfoByUsername(integer, username);
        return info;
    }

/*    *//*
    获取当前用户权限信息
     *//*
    protected UserHasPermissionPojo getUserPermissionInfo(){
        String username=this.getUserName();
        int integer = RegexUtil.validPhoneOrMail(username);
        UserHasPermissionPojo userPermissionInfo=null;
        Object o = redisUtil.get(username);
        if(null!=o){
            return (UserHasPermissionPojo)o;
        }
        userPermissionInfo = usersService.getUserPermissionInfo(integer, username);
        if(null!=userPermissionInfo){
            redisUtil.set(username,userPermissionInfo,24*3600L);
        }


        return userPermissionInfo;
    }*/

    protected boolean getTokenLock(String urlPath){
        RedisLockImpl redisLock = new RedisLockImpl(redisTemplate, 1);
        long lockFlag = redisLock.lock(getUserName() + urlPath, Thread.currentThread().getName());
        if(-1!=lockFlag){
            return true;
        }else{
            return false;
        }
    }

    protected void relesesTokenLock(String urlPath){
        RedisLockImpl redisLock = new RedisLockImpl(redisTemplate, 1);
        redisLock.unlock(getUserName() + urlPath,1L,Thread.currentThread().getName());
    }


}
